package com.code.controller;


import com.code.domain.CommonResult;
import com.code.domain.GpmartUtil;
import com.code.domain.PageObject;
import com.code.entity.Templates;
import com.code.service.ITemplatesService;
import com.code.util.ExportWordUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 合同模版表 前端控制器
 * </p>
 *
 * @author guanfenga@yonyou.com
 * @since 2022-09-15
 */
@Controller
@RequestMapping("/ht")
@Slf4j
public class TemplatesController {


    @Autowired
    private ITemplatesService templatesService;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * http://localhost:9002/mall-user-service/templates/to/toHt
     * @param request
     * @param response
     * @return
     */
    @ApiOperation(value = "跳转到合同列表",notes = "跳转到合同列表")
    @RequestMapping(value = "/to/toHt")
    public String toTemplate( HttpServletRequest request,HttpServletResponse response){
        return "templates/templates_index";
    }
    @ApiOperation(value = "生成word",notes = "生成word")
    @RequestMapping(value = "/exportWord", method = RequestMethod.GET)
    public void exportWord(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String fileName = "测试.doc"; // 文件名称
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("userName","关峰");// 设置要导出的数据,这里的title要和word模板中保持一致
        dataMap.put("teachName","小红");
        dataMap.put("year","2022");
        dataMap.put("month","3");
        dataMap.put("day","1");
        dataMap.put("class","三年级二班");
        dataMap.put("school","光明小学");
        new ExportWordUtil("UTF-8").exportDoc(response, fileName, "template1.ftl", dataMap);
    }


    @ApiOperation(value = "分页查询合同列表",notes = "分页查询合同列表")
    @GetMapping(value = "/getHtPage" )
    public @ResponseBody
    CommonResult getTemplatesPage(PageObject pageObject, Templates templates, HttpServletRequest request, HttpServletResponse response){
        List<Templates> countList = templatesService.queryTemplatesList(templates);
        List<Templates> list = templatesService.findTemplatesPage(GpmartUtil.PageBefore(pageObject.getPage(),pageObject.getLimit()),pageObject.getLimit(),templates);
        return new CommonResult(0 , "操作成功",countList.size(),list );
    }




}
